package com.aviccxzx.smarthome.UserModule;

import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.aviccxzx.smarthome.CommonModule.GosBaseActivity;
import com.aviccxzx.smarthome.R;
import com.aviccxzx.smarthome.netutil.app.App;
import com.aviccxzx.smarthome.utils.DialogUtil;
import com.gizwits.gizwifisdk.api.GizDeviceSharing;
import com.gizwits.gizwifisdk.api.GizDeviceSharingInfo;
import com.gizwits.gizwifisdk.enumration.GizDeviceSharingType;
import com.gizwits.gizwifisdk.enumration.GizWifiErrorCode;
import com.gizwits.gizwifisdk.listener.GizDeviceSharingListener;

import java.util.List;


/**
 * Created by joey on 2016/10/25.
 */
public class ShareRecordAct extends GosBaseActivity implements View.OnClickListener {
    DeviceShareAdapter shareAdapter;
    ListView share_list;
    String token = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setContentView(R.layout.sharelist_lay_v19);
        } else {
        }
        ((TextView) findViewById(R.id.title_tv)).setText("分享记录");
        findViewById(R.id.ll_title_left_view).setOnClickListener(this);
        share_list = (ListView) findViewById(R.id.share_list);
        getShareRecord();
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_title_left_view:
                finish();
                break;
        }
    }

    public void getShareRecord() {
        token = spf.getString("Token", "");
        progressDialog.show();
        GizDeviceSharing.setListener(mListener);
        // 查询自己发出的分享邀请
        GizDeviceSharing.getDeviceSharingInfos(token,
                GizDeviceSharingType.GizDeviceSharingByMe, "");
    }

    // 实现回调
    GizDeviceSharingListener mListener = new GizDeviceSharingListener() {
        @Override
        public void didGetDeviceSharingInfos(GizWifiErrorCode result, String deviceID,
                                             List<GizDeviceSharingInfo> deviceSharingInfos) {
            if (result == GizWifiErrorCode.GIZ_SDK_SUCCESS) {
                // 查询成功
//                App.getApp().showToast("查询成功");
                if (deviceSharingInfos != null && deviceSharingInfos.size() > 0) {
                    shareAdapter = new DeviceShareAdapter(deviceSharingInfos);
                    share_list.setAdapter(shareAdapter);
                }else {
                    App.getApp().showToast("暂时没有分享记录");
                }
            } else {
                // 查询失败
                App.getApp().showToast("查询成功");
            }
            progressDialog.dismiss();
        }
    };

    public class DeviceShareAdapter extends BaseAdapter {
        List<GizDeviceSharingInfo> sharingList;

        public DeviceShareAdapter(List<GizDeviceSharingInfo> deviceSharingInfos) {
            this.sharingList = deviceSharingInfos;
        }

        @Override
        public int getCount() {
            return sharingList.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(final int i, View convertView, ViewGroup viewGroup) {
            View view = null;

            if (convertView != null) {
                view = convertView;
            } else {
                view = LayoutInflater.from(ShareRecordAct.this).inflate(R.layout.sharelist_item_lay, viewGroup, false);
            }

            ViewHolder holder = (ViewHolder) view.getTag();
            if (holder == null) {
                holder = new ViewHolder();
                view.setTag(holder);
            }
            //            BankInfo info = data.get(position);
            //            holder.txt_bankname.setText(info.bankName);
            //            ImageLoader.getInstance().displayImage(ApiType.URL+info.bankLogo, holder.img_bank);
            holder.share_delete_img = (ImageView) view.findViewById(R.id.share_delete_img);
            holder.share_name_tv = (TextView) view.findViewById(R.id.share_name_tv);
            holder.share_name_tv.setText(sharingList.get(i).getUserInfo().getPhone());
            holder.share_delete_img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    new DialogUtil(ShareRecordAct.this, true, true, "确定撤回分享吗?", "取消", "确定", new DialogUtil.SureInterfance() {
                        @Override
                        public void sureTodo() {
                            progressDialog.show();
                            GizDeviceSharing.setListener(revokeListner);
                            // 撤回分享邀请
                            GizDeviceSharing.revokeDeviceSharing(token, sharingList.get(i).getId());
                        }
                        @Override
                        public void cancle() {

                        }
                    }).showCustomDialog();
                }

                GizDeviceSharingListener revokeListner = new GizDeviceSharingListener() {
                    @Override
                    public void didRevokeDeviceSharing(GizWifiErrorCode result, int sharingID) {
                        if (result == GizWifiErrorCode.GIZ_SDK_SUCCESS) {
                            // 撤回成功
                            App.getApp().showToast("撤回成功");
//                            getShareRecord();
                            sharingList.remove(i);
                            shareAdapter.notifyDataSetChanged();
                        } else {
                            // 撤回失败
                            App.getApp().showToast("撤回失败");
                        }
                        progressDialog.dismiss();
                    }
                };
            });
            return view;
        }

        public class ViewHolder {
            TextView share_name_tv;
            ImageView share_delete_img;
        }
    }
}
